package Chart;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

final public class ReadData {

	public double avgcountuser;

	static BufferedReader input;
	// list number of user
	public List<String[]> listUser = new ArrayList<String[]>();

	// list version Iwin
	static List<String> listData = new ArrayList<String>();

	// list time
	public List<Double> listTime = new ArrayList<Double>();

	// data average user
	Map<String, Double> dataAverage = new TreeMap<String, Double>();
	static InputStreamReader isr;

	// data time chart
	public static Map<String, List<Double>> data = new TreeMap<String, List<Double>>();

	// data pie chart
	public static Map<String, Integer> dataPieChartVersion = new TreeMap<String, Integer>();

	// data pie chart 2
	public static Map<String, List<Double>> dataPieChartTime1 = new TreeMap<String, List<Double>>();

	// data pie chart 3
	public static Map<String, List<Double>> dataPieChartTime2 = new TreeMap<String, List<Double>>();

	// data pie chart 4
	public static Map<String, List<Double>> dataPieChartTime3 = new TreeMap<String, List<Double>>();

	// data pie chart 5
	public static Map<String, List<Double>> dataPieChartTime4 = new TreeMap<String, List<Double>>();

	public String file;

	// contructor
	public ReadData() {
	}

	public ReadData(String file) {
		this.file = file;
	}

	// read data from file ofTimeChart
	public void ReadofTimeChart() throws IOException {
		try {
			InputStreamReader isr;
			// isr = new InputStreamReader(new FileInputStream(file));
			// Read data
			isr = new InputStreamReader(new FileInputStream(file));
			BufferedReader input;
			input = new BufferedReader(isr);
			while ((input.readLine()) != null) {
				String read = input.readLine();

				// if (read == null) {
				// continue;
				// }
				// gan data vao list
				listUser.add(read.split("\t"));
			}
			// dua du lieu tu list vao map
			for (String[] str : listUser) {
				String key = str[0].substring(0, str[0].length() - 3);
				try {
					double ccu = Double.valueOf(str[1].substring(2));
					if (data.containsKey(key)) {
						data.get(key).add(ccu);
					} else {
						ArrayList<Double> t = new ArrayList<Double>();
						t.add(ccu);
						data.put(key, t);
					}
				} catch (Exception e) {
					continue;
				}
			}
			// tinh trung binh user/phut
			DecimalFormat twoDForm = new DecimalFormat("#.##");
			for (Entry<String, List<Double>> entry : data.entrySet()) {
				List<Double> d = entry.getValue();
				Double sum = 0.0;
				for (Double i : d) {
					sum += i;
				}
				// Put into dataAverage
				String value = twoDForm.format(sum / d.size())
						.replace(',', '.');
				dataAverage.put(entry.getKey(), Double.valueOf(value));
			}

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void ReadofPieChart() {
		try {
			InputStreamReader isr;
			// isr = new InputStreamReader(new FileInputStream(file));
			// Read data
			isr = new InputStreamReader(new FileInputStream(file));
			BufferedReader input;
			input = new BufferedReader(isr);
			while ((input.readLine()) != null) {
				String read = input.readLine();

				// if (read == null) {
				// continue;
				// }
				// gan data vao list
				listUser.add(read.split("\t"));
			}
			// dua du lieu tu list vao map

			for (String[] str : listUser) {
				String[] temp = str[1].split(" ");
				listData.add(temp[1]);
				double k = Double.valueOf(temp[4]);
				listTime.add(k / 60000);

			}

			// data of version

			for (String str : listData) {
				Integer freq = dataPieChartVersion.get(str);
				dataPieChartVersion.put(str, (freq == null) ? 1 : freq + 1);

			}

			// create key 30 minute
			String key1 = new String("0-30");
			String key2 = new String("30-60");
			String key3 = new String("60-90");
			String key4 = new String(">90");
			for (Double i : listTime) {
				List<Double> lsts = new ArrayList<Double>();
				lsts.add(i);
				if (i <= 30) {
					if (dataPieChartTime1.get(key1) == null) {
						dataPieChartTime1.put(key1, lsts);
					} else {
						dataPieChartTime1.get(key1).add(i);
					}
				} else if (i > 30 && i <= 60) {
					if (dataPieChartTime1.get(key2) == null) {
						dataPieChartTime1.put(key2, lsts);
					} else {
						dataPieChartTime1.get(key2).add(i);
					}
				} else if (i > 60 && i <= 90) {
					if (dataPieChartTime1.get(key3) == null) {
						dataPieChartTime1.put(key3, lsts);
					} else {
						dataPieChartTime1.get(key3).add(i);
					}
				} else if (i > 90) {
					if (dataPieChartTime1.get(key4) == null) {
						dataPieChartTime1.put(key4, lsts);
					} else {
						dataPieChartTime1.get(key4).add(i);
					}
				}
			}
			// create key data 15 minute
			String key5 = new String("0-15");
			String key6 = new String("15-30");
			String key7 = new String("30-45");
			String key8 = new String("45-60");
			String key9 = new String("60-75");
			String key10 = new String("75-90");
			String key11 = new String(">90");

			for (Double i : listTime) {

				List<Double> lsts = new ArrayList<Double>();
				lsts.add(i);
				if (i <= 15) {
					if (dataPieChartTime2.get(key5) == null) {
						dataPieChartTime2.put(key5, lsts);
					} else {
						dataPieChartTime2.get(key5).add(i);
					}
				} else if (i > 15 && i <= 30) {
					if (dataPieChartTime2.get(key6) == null) {
						dataPieChartTime2.put(key6, lsts);
					} else {
						dataPieChartTime2.get(key6).add(i);
					}
				} else if (i > 30 && i <= 45) {
					if (dataPieChartTime2.get(key7) == null) {
						dataPieChartTime2.put(key7, lsts);
					} else {
						dataPieChartTime2.get(key7).add(i);
					}
				} else if (i > 45 && i <= 60) {
					if (dataPieChartTime2.get(key8) == null) {
						dataPieChartTime2.put(key8, lsts);
					} else {
						dataPieChartTime2.get(key8).add(i);
					}
				} else if (i > 60 && i <= 75) {
					if (dataPieChartTime2.get(key9) == null) {
						dataPieChartTime2.put(key9, lsts);
					} else {
						dataPieChartTime2.get(key9).add(i);
					}
				} else if (i > 75 && i <= 90) {
					if (dataPieChartTime2.get(key10) == null) {
						dataPieChartTime2.put(key10, lsts);
					} else {
						dataPieChartTime2.get(key10).add(i);
					}
				} else if (i > 90) {
					if (dataPieChartTime2.get(key11) == null) {
						dataPieChartTime2.put(key11, lsts);
					} else {
						dataPieChartTime2.get(key11).add(i);
					}
				}
			}
			// create key data 5 minute
			String key12 = new String("0-5");
			String key13 = new String("5-10");
			String key14 = new String("10-15");
			String key15 = new String("15-20");
			String key16 = new String("20-25");
			String key17 = new String("25-30");
			String key18 = new String("30-35");
			String key19 = new String("35-40");
			String key20 = new String("40-45");
			String key21 = new String("45-50");
			String key22 = new String("50-55");
			String key23 = new String("55-60");
			String key24 = new String("> 60");
			for (Double i : listTime) {

				List<Double> lsts = new ArrayList<Double>();
				lsts.add(i);
				if (i <= 5) {
					if (dataPieChartTime3.get(key12) == null) {
						dataPieChartTime3.put(key12, lsts);
					} else {
						dataPieChartTime3.get(key12).add(i);
					}
				} else if (i > 5 && i <= 10) {
					if (dataPieChartTime3.get(key13) == null) {
						dataPieChartTime3.put(key13, lsts);
					} else {
						dataPieChartTime3.get(key13).add(i);
					}
				} else if (i > 10 && i <= 15) {
					if (dataPieChartTime3.get(key14) == null) {
						dataPieChartTime3.put(key14, lsts);
					} else {
						dataPieChartTime3.get(key14).add(i);
					}
				} else if (i > 15 && i <= 20) {
					if (dataPieChartTime3.get(key15) == null) {
						dataPieChartTime3.put(key15, lsts);
					} else {
						dataPieChartTime3.get(key15).add(i);
					}
				} else if (i > 20 && i <= 25) {
					if (dataPieChartTime3.get(key16) == null) {
						dataPieChartTime3.put(key16, lsts);
					} else {
						dataPieChartTime3.get(key16).add(i);
					}
				} else if (i > 25 && i <= 30) {
					if (dataPieChartTime3.get(key17) == null) {
						dataPieChartTime3.put(key17, lsts);
					} else {
						dataPieChartTime3.get(key17).add(i);
					}
				} else if (i > 30 && i <= 35) {
					if (dataPieChartTime3.get(key18) == null) {
						dataPieChartTime3.put(key18, lsts);
					} else {
						dataPieChartTime3.get(key18).add(i);
					}
				} else if (i > 35 && i <= 40) {
					if (dataPieChartTime3.get(key19) == null) {
						dataPieChartTime3.put(key19, lsts);
					} else {
						dataPieChartTime3.get(key19).add(i);
					}
				} else if (i > 40 && i <= 45) {
					if (dataPieChartTime3.get(key20) == null) {
						dataPieChartTime3.put(key20, lsts);
					} else {
						dataPieChartTime3.get(key20).add(i);
					}
				} else if (i > 45 && i <= 50) {
					if (dataPieChartTime3.get(key21) == null) {
						dataPieChartTime3.put(key21, lsts);
					} else {
						dataPieChartTime3.get(key21).add(i);
					}
				} else if (i > 50 && i <= 55) {
					if (dataPieChartTime3.get(key22) == null) {
						dataPieChartTime3.put(key22, lsts);
					} else {
						dataPieChartTime3.get(key22).add(i);
					}
				} else if (i > 55 && i <= 60) {
					if (dataPieChartTime3.get(key23) == null) {
						dataPieChartTime3.put(key23, lsts);
					} else {
						dataPieChartTime3.get(key23).add(i);
					}
				} else if (i > 60) {
					if (dataPieChartTime3.get(key24) == null) {
						dataPieChartTime3.put(key24, lsts);
					} else {
						dataPieChartTime3.get(key24).add(i);
					}
				}
			}

			// create key data 5 minute
			String k1 = new String("0-2");
			String k2 = new String("2-4");
			String k3 = new String("4-6");
			String k4 = new String("6-8");
			String k5 = new String("8-10");
			String k6 = new String("10-12");
			String k7 = new String("12-14");
			String k8 = new String("14-16");
			String k9 = new String("16-18");
			String k10 = new String("18-20");
			String k11 = new String("20-40");
			String k12 = new String("40-60");
			String k13 = new String("> 60");

			for (Double i : listTime) {

				List<Double> lsts = new ArrayList<Double>();
				lsts.add(i);
				if (i <= 2) {
					if (dataPieChartTime4.get(k1) == null) {
						dataPieChartTime4.put(k1, lsts);
					} else {
						dataPieChartTime4.get(k1).add(i);
					}
				} else if (i > 2 && i <= 4) {
					if (dataPieChartTime4.get(k2) == null) {
						dataPieChartTime4.put(k2, lsts);
					} else {
						dataPieChartTime4.get(k2).add(i);
					}
				} else if (i > 4 && i <= 6) {
					if (dataPieChartTime4.get(k3) == null) {
						dataPieChartTime4.put(k3, lsts);
					} else {
						dataPieChartTime4.get(k3).add(i);
					}
				} else if (i > 6 && i <= 8) {
					if (dataPieChartTime4.get(k4) == null) {
						dataPieChartTime4.put(k4, lsts);
					} else {
						dataPieChartTime4.get(k4).add(i);
					}
				} else if (i > 8 && i <= 10) {
					if (dataPieChartTime4.get(k5) == null) {
						dataPieChartTime4.put(k5, lsts);
					} else {
						dataPieChartTime4.get(k5).add(i);
					}
				} else if (i > 10 && i <= 12) {
					if (dataPieChartTime4.get(k6) == null) {
						dataPieChartTime4.put(k6, lsts);
					} else {
						dataPieChartTime4.get(k6).add(i);
					}
				} else if (i > 12 && i <= 14) {
					if (dataPieChartTime4.get(k7) == null) {
						dataPieChartTime4.put(k7, lsts);
					} else {
						dataPieChartTime4.get(k7).add(i);
					}
				} else if (i > 14 && i <= 16) {
					if (dataPieChartTime4.get(k8) == null) {
						dataPieChartTime4.put(k8, lsts);
					} else {
						dataPieChartTime4.get(k8).add(i);
					}
				} else if (i > 16 && i <= 18) {
					if (dataPieChartTime4.get(k9) == null) {
						dataPieChartTime4.put(k9, lsts);
					} else {
						dataPieChartTime4.get(k9).add(i);
					}
				} else if (i > 18 && i <= 20) {
					if (dataPieChartTime4.get(k10) == null) {
						dataPieChartTime4.put(k10, lsts);
					} else {
						dataPieChartTime4.get(k10).add(i);
					}
				} else if (i > 20 && i <= 40) {
					if (dataPieChartTime4.get(k11) == null) {
						dataPieChartTime4.put(k11, lsts);
					} else {
						dataPieChartTime4.get(k11).add(i);
					}
				} else if (i > 40 && i <= 60) {
					if (dataPieChartTime4.get(k12) == null) {
						dataPieChartTime4.put(k12, lsts);
					} else {
						dataPieChartTime4.get(k12).add(i);
					}
				} else if (i > 60) {
					if (dataPieChartTime4.get(k13) == null) {
						dataPieChartTime4.put(k13, lsts);
					} else {
						dataPieChartTime4.get(k13).add(i);
					}
				}
			}

		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
